# -*- coding: utf-8 -*-
# @Author: QX
# @File: ApiTestRunner.py
'''
所有的测试用例的入口：
主要目的：实现一个公共的用来分解测试步骤的入口
1.引入测试用例
2.拿到测试用例的标题
3.分解步骤
4.调用关键字，实现关键字对应的功能
'''
import copy

import allure
import pytest

from core.globalContext import g_context
from extend.keyworks import Keywords
from parse.ExcelCaseParser import load_excel_files, excel_case_parser
from parse.YamlCaseParser import readYamlnew, load_context_from_yaml, load_yaml_files, yaml_case_parser
from utils.DynamicTitle import dynamicTitle
from utils.VarRender import ReFresh


class TestRunner:
    # folder_path = 'D:\Apirun2411\examples\yaml_testcase'
    #
    # g = yaml_case_parser(folder_path)
    # caseinfo = g['case_infos']
    # folder_path = 'D:\Apirun2411\examples\excel_testcase'
    # caseinfo = excel_case_parser(folder_path)
    # caseinfo = caseinfo['case_infos']


    #@pytest.mark.parametrize('caseinfo',caseinfo)
    def test_case_execute(self, get_dict_token, caseinfo):
        # 2.拿到测试用例的标题
        try:
            #直接在测试用例中获得
            #allure.dynamic.title(caseinfo['_case_name'])
            #自定义用例标题
            dynamicTitle(caseinfo)
            local_context = caseinfo.get('context',{})
            kw = Keywords()
            # 3.分解步骤
            #3.1获得测试用例中的步骤
            steps=caseinfo.get('steps',None)
            for step in steps:
                #获得每一步的步骤名称
                step_name=list(step.keys())[0]
                step_value = list(step.values())[0]
                print('step_name=====',step_name)
                print('step_value====',step_value)
                context = copy.deepcopy(g_context().show_dic())

                print('公共变量dict--context=====',context)
                context.update(local_context)
                step_value = eval(ReFresh(step_value, context))
                print(f'{caseinfo["desc"]}step_value=====',step_value)
                with allure.step(step_name):
                    key=step_value['关键字']
                    try:
                        key_func=kw.__getattribute__(key)
                    except  Exception  as e:
                        print(e)

                    key_func(**step_value)
        except Exception  as e:
            assert False , f"核心执行器或是核心执行器调用的关键字有错误{e}"
        finally:
            print('---执行用例完成---')


        # 4.调用关键字，实现关键字对应的功能


if __name__ == '__main__':
    pytest.main(['-vs',__file__])